import matplotlib

matplotlib.use('TkAgg')

import matplotlib.pyplot as plt
import numpy as np

import pandas as pd

df = pd.read_excel('./results.xlsx')
dict_list = df.to_dict(orient='records')

tasks = df['task'].unique()
models = df['model'].unique()

aurocs = {model: [] for model in models}
cis = {model: [] for model in models}

for task in tasks:
    task_data = df[df['task'] == task]
    for model in models:
        value_df = task_data[task_data['model'] == model]
        aurocs[model].append(value_df['acc'].values[0])
        cis[model].append(value_df['ci'].values[0])

# models = ['CRISP', 'Virchow2', 'Virchow', 'UNI', 'ours']
# tasks = ['ER', 'Grade', 'HER2', 'Ki67', 'pCR', 'PR']

# aurocs = np.array([
#     # Task1, Task2, Task3, Task4, Task5, Task6
#     [0.85, 0.87, 0.83, 0.89, 0.86, 0.88],
#     [0.82, 0.85, 0.81, 0.87, 0.84, 0.86],
#     [0.88, 0.90, 0.86, 0.91, 0.89, 0.90],
#     [0.80, 0.83, 0.79, 0.85, 0.82, 0.84],
#     [0.86, 0.88, 0.84, 0.90, 0.87, 0.89],
# ])
#
# cis = np.array([
#     [0.03, 0.04, 0.03, 0.02, 0.03, 0.03],
#     [0.04, 0.03, 0.04, 0.03, 0.04, 0.03],
#     [0.02, 0.02, 0.03, 0.02, 0.02, 0.02],
#     [0.05, 0.04, 0.05, 0.04, 0.05, 0.04],
#     [0.03, 0.03, 0.03, 0.03, 0.03, 0.03],
# ])

# 设置图形
fig, ax = plt.subplots(figsize=(16, 8))

# 颜色方案
colors = ['#5B4E9D', '#E74C9C', '#FF6B9D', '#FF8B6B', '#FFB84D', '#F4E04D']

# 设置柱子的宽度和位置
bar_width = 0.15
x_pos = np.arange(len(tasks))

# 为每个方法绘制柱状图
for i, model in enumerate(models):
    # 计算每个方法的x位置偏移
    offset = (i - len(models) / 2 + 0.5) * bar_width
    positions = x_pos + offset

    # 提取当前方法的AUC分数和置信区间
    model_aucs = aurocs[model]
    model_cis = cis[model]

    # 绘制柱状图
    if 'ours' in model:
        bars = ax.bar(positions, model_aucs, bar_width,
                  label=model, hatch='//', color=colors[i], alpha=0.8)
    else:
        bars = ax.bar(positions, model_aucs, bar_width,
                      label=model, color=colors[i], alpha=0.8)

    # 添加误差线（95%置信区间）
    ax.errorbar(positions, model_aucs, yerr=model_cis,
                fmt='none', ecolor='black', capsize=4,
                capthick=1.5, alpha=0.6, linewidth=1.5)

ax.set_xticks(x_pos)
ax.set_xticklabels(tasks, fontsize=12)

# 设置y轴
ax.set_ylabel('ACC', fontsize=14, fontweight='bold')
ax.set_ylim([0, 1])
ax.tick_params(axis='y', labelsize=11)

ax.grid(axis='y', alpha=0.3, linestyle='--', linewidth=0.8)

ax.legend(loc='upper right', fontsize=11, frameon=False, ncol=5)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.tight_layout()
# plt.show()
plt.savefig("png_bar_acc")
